Why JavaScript Statement "ga = ga || []" Works?
Posted
by Morgan Cheng
on Stack Overflow
See other posts from Stack Overflow
or by Morgan Cheng
Published on 2010-04-21T03:31:39Z
Indexed on
2010/04/21
3:43 UTC
Read the original article
Hit count: 418
JavaScript
|syntax
Below javascript statements will cause error, if ga is not declared.
if (ga)
{
alert(ga);
}
The error is:
ga is not defined
It looks undeclared variable cannot be recognized in bool expression. So, why below statement works?
var ga = ga || [];
To me, the ga is treated as bool value before "||". If it is false, expression after "||" is assigned to final ga.
© Stack Overflow or respective owner